package cn.ranyue.org.controller;

import cn.ranyue.basic.util.AjaxResult;
import cn.ranyue.basic.util.QueryResult;
import cn.ranyue.org.domain.Systemdictionarytype;
import cn.ranyue.org.query.SystemdictionarytypeQuery;
import cn.ranyue.org.service.ISystemdictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemdictionaryType")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class SystemdictionarytypeController {
   @Autowired
   private ISystemdictionarytypeService systemdictionarytypeService;

   @GetMapping("/{id}")
   @ApiOperation(value = "通过id查询某一条数据")
    public Systemdictionarytype queryById(@PathVariable Long id){
        return systemdictionarytypeService.loadOne(id);

    }
    /*

    * 查询所有
    * 此时的请求方式：get请求 http://locallhost:8080/dept
    * */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "查询所有")
    public List<Systemdictionarytype> queryAll(){
       return systemdictionarytypeService.loadAll();
    }
    /*分页查询
    * @RequestBody：从请求体获取jsonge
    * 此时的请求方式：post请求 http://locallhost:8080/dept
    * */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public QueryResult<Systemdictionarytype> queryPage(@RequestBody SystemdictionarytypeQuery query){
       return systemdictionarytypeService.queryPage(query);

    }
    /*
    * 删除一条数据
    * 此时的请求方式：delete请求 http://locallhost:8080/dept/id
    * */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过删除某一条数据")
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            systemdictionarytypeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
           e.printStackTrace();
           return AjaxResult.me().setSuccess(false).setMassage("操作失败");
        }
    }
    /*
    * 添加和修改数据
    * 此时的请求方式：put请求 http://locallhost:8080/dept/id
    * 又id为修改数据 没有id为添加数据
    * */
    @PutMapping
    @ApiOperation(value = "添加或者修改一条数据")
    public AjaxResult addAndupdate(@RequestBody Systemdictionarytype systemdictionarytype){
        try {
            if(systemdictionarytype.getId()==null){
                systemdictionarytypeService.add(systemdictionarytype);
            }else{
                systemdictionarytypeService.update(systemdictionarytype);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("操作失败");
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            systemdictionarytypeService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("操作失败");
        }
    }

}
